package cz.muni.fi.pa165.rentalofconstructionmachinery.dao.impl;

import cz.muni.fi.pa165.rentalofconstructionmachinery.dao.RevisionDAO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Machine;
import cz.muni.fi.pa165.rentalofconstructionmachinery.entity.Revision;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Adam Zmrzlý
 * @version Aventador (20121103)
 */
@Repository(value="revisionDao")
public class RevisionDAOImpl implements RevisionDAO {

    @PersistenceContext
    private EntityManager em;

    private static final Logger logger = LoggerFactory.getLogger(RevisionDAOImpl.class);

    @Override
    public void createRevision(Revision revision) {
        if(revision == null) {
            throw new IllegalArgumentException("Revision cannot be null.");
        }
        if(revision.getMachine() == null) {
            throw new IllegalArgumentException("Machine related to this revision cannot be null.");
 	}
        logger.debug("Creating a new Revision - {}", revision);
        em.persist(revision);
    }

    @Override
    public void deleteRevision(Revision revision) {
        if(revision == null) {
            throw new IllegalArgumentException("Revision cannot be null.");
        }

        logger.debug("Deleting a Revision - {}", revision);
        em.remove(em.merge(revision));
    }

    @Override
    public void updateRevision(Revision revision) {
        if(revision == null) {
            throw new IllegalArgumentException("Revision cannot be null.");
        }
        if(revision.getMachine() == null) {
            throw new IllegalArgumentException("Machine related to this revision cannot be null.");
 	}

        logger.debug("Updating a Revision - {}", revision);
        em.merge(revision);
    }

    @Override
    public List<Revision> getAllRevisions() {
        logger.debug("Getting all Revisions");
        List<Revision> ret = em.createQuery("SELECT r FROM Revision r", Revision.class).getResultList();

        return ret;
    }

    @Override
    public Revision getRevisionById(Long id) {
        if(id == null) {
            throw new IllegalArgumentException("Id cannot be null.");
        }
        logger.debug("Getting Revision by id {}", id);

        Revision ret = em.createQuery("SELECT r FROM Revision r WHERE id =" + id, Revision.class).getSingleResult();

        return ret;
    }

    @Override
    public Revision getLastRevisionOfMachine(Machine machine) {
        if(machine == null) {
            throw new IllegalArgumentException("Machine cannot be null.");
        }
        logger.debug("Getting the last Revision of Machine {}", machine);

        Revision ret = em.createQuery("SELECT r FROM Revision r, Machine m WHERE m.id = " + machine.getId() + " AND r.machine.id = m.id ORDER BY dateOfRevision DESC", Revision.class).setMaxResults(1).getSingleResult();
        
        return ret;
    }

    @Override
    public List<Revision> getAllRevisionsOfMachine(Machine machine) {
        logger.debug("Getting all Revisions of Machine {}", machine);

        List<Revision> ret = em.createQuery("SELECT r FROM Revision r WHERE r.machine.id = " + machine.getId(), Revision.class).getResultList();

        return ret;
    }

}
